package pl.smath.regex;

import junit.framework.TestCase;
import pl.smath.expression.model.NumberTerm;
import pl.smath.expression.model.Term;
import pl.smath.expression.model.VariableTerm;
import pl.smath.expression.parser.ExpressionParser;
import pl.smath.expression.parser.ExpressionParserException;
import pl.smath.regex.exceptions.NoMatchFound;

public class FullUseTestCase extends TestCase {

	/** 
     *  Tests getting information about terms from matched expressions.
     *
     *  testNumbers -  {"expression", "matching term", "number label", "valid value"}
     *  testVariable - {"expression", "matching term", "variable label", "valid value"}
     */

	
   private static final String[][] TEST_DATA = {
   		{"2*Number(a)*4", "2*(3)*4","a","3"},
   		{"1*2*(3*4)+Number(a)", "1*2*(3*4)+5","a","5"},
   		{"(2+Number(a))/(Variable(x)*5)", "(2 + 3)/(y*5)", "a", "3"},
       	{"Number(a)*Variable(y)[=2]", "5*x = 2", "a", "5"},
     	{"Number(a)^Variable(x) + Number(z)", "2^x + 3", "z", "3"},
   		{"(2+Number(a))/(5*Variable(x)) > Variable(t)", "(2 + 3)/(5*y) > z", "a", "3"},
   		{"Sum{n=0, n=infinity}{Number(a_n)*Variable(x)^n}", "2y^3+3y^2+4y+5", "a_1", "4"},
   };
   
   private static final String[][] TEST_DATA_VAR = {
		{"(2+Number(a))/(Variable(x)*5)", "(2 + 3)/(y*5)", "x", "y"},
       	{"Number(a)*Variable(y)[=2]", "5*x = 2", "y", "x"},
     	{"Number(a)^Variable(x) + Number(z)", "2^x+3", "x", "x"},
   		{"(2+Number(a))/(5*Variable(x)) > Variable(t)", "(2 + 3)/(5*y) > z", "t", "z"},
   		{"Sum{n=0, n=infinity}{Number(a_n)*Variable(x)^n}", "2y^3+3y^2+4y+5", "x", "y"},
  		{"Sum{n=0, n=infinity}{Variable(g)^n}", "y^3+y^2+y+1", "g","y"},
   };

   public void testNumbers()  throws ExpressionParserException{
   		PatternBuilder pb = new PatternBuilder();
		Pattern p;
		ExpressionParser parser = new ExpressionParser();
       
       for(int i = 0; i < TEST_DATA.length; i++){
       		try {
       			p  = pb.compile(TEST_DATA[i][0]);
       			Term term = parser.parseText(TEST_DATA[i][1]);
   				Expression e = p.process(term); 
   				assertNotNull(e);
   				NumberTerm n = e.getNumber(TEST_DATA[i][2]);
   				if (n.toString().equals(TEST_DATA[i][3]))
   					System.err.println("TESTCASE " + i + " OK.");
   				else
   					System.err.println("TESTCASE " + i + " WRONG.");
       		}
       		catch (NoMatchFound error) {
       			assertFalse("NoMatchFound Error" + error.getMessage(), false);
       			System.err.println("TESTCASE " + i + " FAILED.");
       		}
       		
       }
   }
   
   public void testVariables()  throws ExpressionParserException{
		PatternBuilder pb = new PatternBuilder();
		Pattern p;
		ExpressionParser parser = new ExpressionParser();
      
		for(int i = 0; i < TEST_DATA_VAR.length; i++){
			try {
				p  = pb.compile(TEST_DATA_VAR[i][0]);
		  		Term term = parser.parseText(TEST_DATA_VAR[i][1]);
				Expression e = p.process(term); 
				assertNotNull(e);
				VariableTerm n = e.getVariable(TEST_DATA_VAR[i][2]);
				if (n.toString().equals(TEST_DATA_VAR[i][3]))
					System.err.println("TESTCASE2 " + i + " OK.");
				else
					System.err.println("TESTCASE2 " + i + " WRONG.");
			}
			catch (NoMatchFound error) {
				assertFalse("NoMatchFound Error" + error.getMessage(), false);
				System.err.println("TESTCASE2 " + i + " FAILED.");
		  	}
		}
  }

   
}
